<!--教学任务查询-->
<template>
<div id="prof-course">
  <div class="assessment-container">
    <div class="search-container">
      <div class="search-container-left">
        <div class="search-container-item">
          <span>学期：</span>
          <el-select
            v-model="xq"
            style="width: 90px"
            size="mini"
            placeholder="请选择"
            @change="changeXueQi"
          >
            <el-option
              v-for="item in xqList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="search-container-right">
        <el-button type="warning" plain @click="syncShow" icon="el-icon-refresh" size="mini">同步</el-button>
        <el-button type="warning" plain @click="schemeImport" icon="el-icon-download" size="mini">从培养方案导入</el-button>
        <el-button type="warning" plain @click="addCourse" icon="el-icon-circle-plus-outline" size="mini">添加课程</el-button>
        <el-button type="danger" plain @click="deletedCourseBatch" icon="el-icon-delete" size="mini">删除选中</el-button>
      </div>
    </div>
  </div>

  <div class="assessment-container">
      <div class="table-container">
        <el-table
        :data="tableData"
        border
        style="width: 100%;height: calc(100vh - 350px);overflow-y: auto;"
        class="table-data"
        row-class-name="row-class"
        cell-class-name="cell-class"
        header-row-class-name="header-row-class"
        header-cell-class-name="header-cell-class"
        @selection-change="handleSelectionChange"
        :row-key="getRowKeys"
      >
      <el-table-column>
        <template slot="header">
               <el-row style="width: 80%;text-align: center;">
                   <el-col :span="5"><span>专业: {{ specialtyName }}</span></el-col>
                   <el-col :span="5"><span>专业班次: {{ className }}</span></el-col>
                   <el-col :span="4"><span>额定人数: {{ ratedNumber }}人</span></el-col>
                   <el-col :span="10">
                     <el-button type="primary" plain @click="beforeExePlan" icon="el-icon-search" size="mini">查看以往班次执行计划</el-button>
                     </el-col>
               </el-row>
           </template>
          <el-table-column  width="100" align="center" :reserve-selection="true"  type="selection"></el-table-column>
            <el-table-column prop="kcbh" label="课程编号" align="center"></el-table-column>
            <el-table-column prop="kcmc" label="课程名称" align="center"></el-table-column>
            <el-table-column prop="cddw" label="承担单位"  align="center"></el-table-column>
            <el-table-column prop="jhskxq" label="计划上课学期" align="center"></el-table-column>
            <el-table-column label="学分"  align="center">
                <template slot-scope="{row}">
                    <el-input onkeyup="value=value.replace(/[^\d]/g,'')" size="small" v-model="row.xf" @change="handleUpdateScore(row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="讲授课时" width="187.5" align="center">
                <template slot-scope="{row}">
                    <el-input onkeyup="value=value.replace(/[^\d]/g,'')" size="small" v-model="row.jsxs" @blur="calculate(row)" @change="handleUpdateScore(row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="实践课时" width="187.5" align="center">
                <template slot-scope="{row}">
                    <el-input onkeyup="value=value.replace(/[^\d]/g,'')" size="small" v-model="row.sjxs" @blur="calculate(row)" @change="handleUpdateScore(row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="zxs" label="总学时" align="center"></el-table-column>
            <el-table-column prop="cz" label="操作" width="191" align="center">
                <template slot-scope="scope">
                    <el-button type="danger" plain icon="el-icon-circle-close" @click="deleteBtn(scope.row)" size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table-column>
      </el-table>
      </div>
    </div>

    <div class="wrapper-container">
      <div class="bottom-block" style="display: block">
        <div class="text">当前共找到{{ total }}条记录,当前显示{{ startPage }}-{{ endPage }}条</div>
      </div>
      <div class="pagination-block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
        >
        </el-pagination>
      </div>
    </div>
    <div style="margin-top: 30px;text-align: center;">
      <el-button type="warning" plain size="mini" @click="submitCourse">完 成</el-button>
    </div>

    <el-dialog title="提示" :visible.sync="schemeShowTip" width="800px" class="deletedDialog">
      <div class="class-tip">
        确定从培养方案导入么？
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="warning" plain size="mini" style="margin-right: 50px" @click="submitExportConfirm">确 定</el-button>
        <el-button type="danger" plain size="mini" @click="schemeShowTip = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="syncShowTip" width="800px" class="deletedDialog">
      <div class="class-tip">
        一旦同步，数据将不能恢复，您确定要同步么？
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="warning" plain size="mini" style="margin-right: 50px" @click="tipConfirm">确 定</el-button>
        <el-button type="danger" plain size="mini" @click="syncShowTip = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="deletedInfoDialog" width="600px" class="deletedDialog">
      <div class="class-tip">
        您确认删除当前信息吗？
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="warning" plain size="mini" style="margin-right: 50px" @click="deletedSubmit">确 定</el-button>
        <el-button type="danger" plain size="mini" @click="deletedCancel">取 消</el-button>
      </span>
    </el-dialog>
</div>
</template>

<script>
import {queryZybcCourseByZybcId, updateZxjhCourse, deleteZxjhCourse, addFromPykcByZybcId, queryCommonXueqi, zdzybc_tongbu_Course_zybc, specialtyclassinfo_detail, importAddPykcByZybcId} from "@/api/trainingManagement/place.js";
export default {
  data() {
    return {
      deletedInfoDialog: false, //删除信息弹窗
      syncShowTip: false,
      schemeShowTip: false,
      xq: "",
      xqList: [],
      tableData: [],
      kcId: "", //课程id
      multipleSelection:[],//多选
      deletedIdStr: [],
      specialtyName: "计算机专业",
      className: "计算机专业",
      ratedNumber: "1",
      total: 0, //总条数
      pageSize: 50, //条数
      startPage: 0, //开始条数
      endPage: 0, //结束条数
      currentPage: 1, //当前页数
    };
  },
  mounted() {
    this.kcId = this.$route.query.id;
    this.xq = this.$route.query.xq;
    this.queryTopData()
    this.queryCommonXueqi()
    this.queryZdzybcxqzxjhList();
  },
  methods: {
    handleUpdateScore(row){
      let data = [
        {
          id: row.id,
          xf: row.xf,
          jsxs: row.jsxs,
          sjxs: row.sjxs
        }
      ]
      updateZxjhCourse(data).then(res=>{
        if(res.data.code == 200){
          console.log('修改成功')
          this.$message.success('修改成功')
        }
      })
    },
    getRowKeys(row) {
      return row.id
    },
    // 获取学期列表
    async queryCommonXueqi() {
      const res = await queryCommonXueqi();
      if (res.data.code == 200) {
        this.xqList = res.data.data;
      }
    },
    // 学期改变
    changeXueQi() {
      this.queryZdzybcxqzxjhList();
    },
    // 批量从培养方案导入
    async submitExportConfirm() {
      var zybcId = this.kcId;
      var pykcId = ""
      this.multipleSelection.forEach(item => {
        pykcId += item.id + ","
      })
      pykcId = pykcId.substring(0, pykcId.length - 1);
      const res = await importAddPykcByZybcId(pykcId, zybcId, this.xq);
      if (res.data.code == 200) {
        this.$message.success("导入成功!");
        this.schemeShowTip = false;
        this.queryZdzybcxqzxjhList();
      }
    },
    deletedCancel() {
      this.deletedInfoDialog = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deletedCourseBatch() {
      if(this.multipleSelection.length > 0) {
        this.deletedIdStr = [];
        var idArr = [];
        this.multipleSelection.forEach(item => {
          idArr.push(item.id);
        })
        this.deletedIdStr = idArr;
        this.deletedInfoDialog = true;
      } else {
        this.$message.warning("请至少选中一条数据！");
      }
    },
    // 批量删除
    async deletedSubmit() {
        const res = await deleteZxjhCourse(this.deletedIdStr);
        if (res.data.code == 200) {
          this.$message.success("删除成功!");
          this.deletedInfoDialog = false;
          this.queryZdzybcxqzxjhList();
        }
    },
    deleteBtn(row) {
      this.deletedIdStr = [row.id];
      this.deletedInfoDialog = true;
    },
    // 完成操作，保存编辑后的课程数据
    async submitCourse() {
      if (this.tableData.length > 0) {
        var newArr = [];
        this.tableData.forEach(item => {
          newArr.push({
            "id": item.id,
            "xf": item.xf,
            "jsxs": item.jsxs,
            "sjxs": item.sjxs
          })
        })
        const res = await updateZxjhCourse(newArr);
        if (res.data.code == 200) {
          this.$message.success("保存成功!");
          this.$router.push("/admin/teachingTaskManage/termExecutionPlan")
        }
      }
    },
    calculate(row) {
      row.zxs =  parseFloat(row.jsxs) + parseFloat(row.sjxs)
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryZdzybcxqzxjhList();
    },
    queryTopData() {
      var zybcId = this.kcId;
      specialtyclassinfo_detail(zybcId).then((res)=> {
        if (res.data.code == 200) {
            this.specialtyName = res.data.data.specialtyName;
            this.className = res.data.data.className;
            this.ratedNumber = res.data.data.ratedNumber;
          this.$nextTick(() => {
            // this.$set(this.formData,"specialtyName",res.data.data.specialtyName);
            // this.$set(this.formData,"className",res.data.data.className);
            // this.$set(this.formData,"ratedNumber",res.data.data.ratedNumber);
            // // console.log(this.specialtyName, "llllllllll")
            // console.log(this.formData)
          })

        }
      })
    },
    // 查询专业班次列表信息
    async queryZdzybcxqzxjhList() {
      var zybcId = this.kcId;
      var xueqi = this.xq;
      var current = this.currentPage;
      var size = this.pageSize;
      const res = await queryZybcCourseByZybcId(zybcId, xueqi, current, size);
      if (res.data.code == 200) {
        this.tableData = res.data.data.records;
        this.total = res.data.data.total;
        var length = this.tableData.length;
        this.calculatePage(
          this.pageSize,
          res.data.data.current, length);
      }
    },
    // 计算开始和结束条数
    calculatePage(pageSize, current, length) {
      if (current == 1) {
        if (length != 0) {
          this.startPage = 1;
          this.endPage = length;
        }
      } else {
        this.startPage = pageSize * (current - 1) + 1;
        this.endPage = parseInt(this.startPage)  + parseInt(length) - 1;
      }
    },
    addCourse(){
      this.$router.push({
        path: "/admin/teachingTaskManage/addCourse",
        query: {
          kcId: this.kcId,
          xueqi:this.xq
        }
      })
    },
    beforeExePlan(){
        this.$router.push({
          path: "/admin/teachingTaskManage/beforeExecutePlan",
          query: {
            kcid: this.kcId
          }
        })
    },
    schemeImport(){
      this.schemeShowTip = true;
      // if(this.multipleSelection.length > 0) {
      //
      // } else {
      //   this.$message.warning("请至少选中一条数据！");
      // }
    },
    async tipConfirm(){
      if (this.xq == "" || this.xq == null) {
        this.$message.warning("请先选择学期!");
        return;
      }
      var kcydList = [];
      this.multipleSelection.forEach(item => {
        kcydList.push(item.kcid)
      });
      var params = {
          "xueqi": this.xq,
          "list":[this.kcId],
          "kcIdList":kcydList,
          "zybcId": this.kcId
      }
      const res = await zdzybc_tongbu_Course_zybc(params);
      if (res.data.code == 200) {
        this.$message.success("同步成功！");
        this.syncShowTip = false;
        this.queryZdzybcxqzxjhList();
      }
    },
    syncShow(){
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择课程数据!");
        return;
      }
      this.syncShowTip = true;
    },
    addHours() {
      this.$router.push("/teacher/workload/ClassTimeQuery");
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../../styles/delayedExam.less";
@import "../../../../styles/text.less";
@import "../../../../styles/supervision.less";
.pagination-block {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.deletedDialog {
  /deep/ .el-dialog {
    height: 320px;
  }
}


/deep/ .table-container {
  .el-table td,
  .el-table th.is-leaf,
  .el-table--border,
  .el-table--group {
    border-color: #c7d7eb;
  }

  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 76px;
    background-color: #edf2f9;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    line-height: 20px;
    color: #004ca7;
  }

  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 86px;
  }

  .wrapper-button {
    display: flex;
    flex-direction: column;
    align-items: center;

    > div {
      margin-bottom: 8px;
    }
  }
  .table-data {
    width: 100%;
    border: 0.8px solid #004ca7;
    /*border-radius: 9px;*/
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #004ca7;
  }
}

.assessment-container {
  width: calc(100% - 20px);
  .table-container {
     width: 100%;
  }
}

.bottom-block{
  width: calc(100% - 20px);
  margin: 0 auto;
}

 /deep/ .header-row-class {
   > th {
     border-bottom: 1px solid #c7d7eb;
   }
 }

 .class-tip {
  color: #004ca7;
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  margin: 40px;
}
</style>


<style lang="less" scoped>
  #prof-course{
    /deep/ .el-button--warning.is-plain{
      background: #FFF6EF;
      border-color: #F3B815;
      color: #F3B815;
      border-radius: 6px;
    }
    /deep/ .el-button--warning.is-plain:focus, .el-button--warning.is-plain:hover{
      color: #F3B815;
      background: #FFF6EF;
      border: 1px solid #F3B815;
      border-radius: 6px;
    }

    /deep/ .el-button--danger.is-plain{
      background: #FFEFEF;
      border-color: #FF3819;
      color: #FF3819;
      border-radius: 6px;
    }

    /deep/ .el-button--danger.is-plain:hover{
      background: #FFEFEF;
      border: 1px solid #FF3819;
      color: #FF3819;
      border-radius: 6px;
    }

    /deep/ .el-button--primary.is-plain{
      background: #F1F8FE;
      border-color: #51AEF6;
      color: #51AEF6;
      border-radius: 6px;
    }

    /deep/ .el-button--primary.is-plain:hover{
      background: #F1F8FE;
      border: 1px solid #51AEF6;
      color: #51AEF6;
      border-radius: 6px;
    }
  }
</style>
